package cn.dnui_xqh414.dnuifood_16110100414.fragment_adapter_and_vhoder;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import cn.dnui_xqh414.dnuifood_16110100414.R;
import cn.dnui_xqh414.dnuifood_16110100414.bean.getAllCommentsBean;
import cn.dnui_xqh414.dnuifood_16110100414.model.FoodlistModel;

public class rv_Adapter_foodComments
        extends RecyclerView.
        Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<getAllCommentsBean> list;
    private LayoutInflater inflater;

    private FoodlistModel model;

    public rv_Adapter_foodComments(Context context){
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<getAllCommentsBean> list){
        this.list = list;

        notifyDataSetChanged();   //十分重要
    }

    class FoodCommentsViewHolder extends
            RecyclerView.ViewHolder {

        View itemView;

        TextView username,comments_time,content;
        ImageView imageView;

        public FoodCommentsViewHolder(@NonNull View itemView) {
            super(itemView);
            username = itemView.findViewById(R.id.tv_food_userName);
            comments_time = itemView.findViewById(R.id.tv_comment_date);
            content = itemView.findViewById(R.id.tv_food_comment);
            imageView = itemView.findViewById(R.id.img_foodComment);

            this.itemView = itemView;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.rv_item_food_comment,
                        parent ,false);
        return new FoodCommentsViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final getAllCommentsBean commentsBean = list.get(position);
        final FoodCommentsViewHolder viewHolder = (FoodCommentsViewHolder) holder;

        if (commentsBean == null) return;

        viewHolder.comments_time.setText(commentsBean.getComment_time());
        viewHolder.username.setText(String.valueOf(commentsBean.getUser_id()));
        viewHolder.content.setText(commentsBean.getContent());
//        Picasso.get().load(R.drawable.ic_comment_holder).fit().centerCrop().into(viewHolder.imageView);
        viewHolder.imageView.setImageResource(R.drawable.ic_comment_holder);
    }

    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        return list.size();
    }
}
